﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Person;
using BrokerBase.Lib.Core;
using NHibernate;

namespace Broker.View.PersonView
{
    /// <summary>
    /// HumanRelationTypeList.xaml 的交互逻辑
    /// </summary>
    public partial class HumanRelationTypeListPage
    {
        public HumanRelationTypeListPage()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(HumanRelationTypeList_Loaded);
        }

        void HumanRelationTypeList_Loaded(object sender, RoutedEventArgs e)
        {
            Query();
        }


        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            
            IHumanRelationType humanRelationType = new HumanRelationType();

            HumanRelationTypeForm frm = new HumanRelationTypeForm(humanRelationType, new Action(Query));
            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = LibGlobal.MainWin;
            //frm.Width = 360;
            //frm.Width = 260;
            frm.ShowDialog();
        }

        private void benDel_Click(object sender, RoutedEventArgs e)
        {
            IHumanRelationType humanRelationType = gvHumanRelation.SelectedItem as IHumanRelationType;

            if (humanRelationType != null)
            {
                if (MessageBox.Show("是否要删除" + humanRelationType.Name + "?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    try
                    {
                        IBaseService<HumanRelationType> service = new BaseService<HumanRelationType>(LibGlobal.HbmSessionFactory.GetCurrentSession());

                        service.Delete(humanRelationType as HumanRelationType);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }



        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            ViewHumanRelationType();
        }


        private void gvHumanRelation_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ViewHumanRelationType();
        }

        private void ViewHumanRelationType()
        {
            IHumanRelationType humanRelationType = gvHumanRelation.SelectedItem as IHumanRelationType;

            if (humanRelationType != null)
            {
                HumanRelationTypeForm frm = new HumanRelationTypeForm(humanRelationType,new Action(Query));
                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = LibGlobal.MainWin;
                frm.ShowDialog();
            }
        }

        private void Query()
        {
            try
            {
                ISession sess = LibGlobal.HbmSessionFactory.GetCurrentSession();

                IList<IHumanRelationType> ls = sess.CreateQuery("from HumanRelationType").List<IHumanRelationType>();
                gvHumanRelation.ItemsSource = ls;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool CheckUsed(IHumanRelationType humanRelationType)
        {
            //string sql = "select count(*) from ";

            return true;
        }
    }
}
